Private Sub Command1_Click()
    For strIndex = 1 To Len(Class_Grade.Text)
        If Mid(Class_Grade.Text, strIndex, 1) = " " Then
            MsgBox "班级名称中不能出现空格，请检查后重试", vbOKOnly + vbExclamation, "提示"
            Exit Sub
        End If
    Next

    If Class_Grade.Text = "" Then
        MsgBox "班级名称不能为空", vbOKOnly + vbExclamation, "温馨提示"
    Else
        'create a drive string
        db_password = Password.Text
        table_name = Class_Grade.Text
        If db_password = "" Then
            If Dir(db_name & Class_Grade.Text & ".mdb") = "" Then
                Dim isPassword
                isPassword = MsgBox("密码为空，安全性较低，是否继续？", _
                    vbYesNo + vbExclamation, "温馨提示")
                If isPassword = vbYes Then
                    db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                        db_name & table_name & ".mdb" & Chr(34)
                    If Dir(db_name & Class_Grade.Text & ".mdb") = "" Then
                        Set catalog = New adox.catalog
                        Set db_table = New adox.Table
                        On Error GoTo DatabaseError
                        catalog.Create db_drive
                        catalog.ActiveConnection = db_drive
                        table_name = Class_Grade.Text
                        db_table.Name = table_name
                        db_table.Columns.Append "index", adox.DataTypeEnum.adInteger, 10
                        db_table.Columns.Append "id", adox.DataTypeEnum.adInteger, 10
                        db_table.Columns.Append "name", adox.DataTypeEnum.adWChar, 10
                        db_table.Columns.Append "moral_score", adox.DataTypeEnum.adInteger
                        db_table.Columns.Append "explicit", adox.DataTypeEnum.adWChar, 200
                        catalog.Tables.Append db_table
                        If Dir(db_name & table_name & "德育分细则\", vbDirectory) = "" Then
                            MkDir db_name & table_name & "德育分细则\"
                        End If
                        manage.Show
                        Unload Me
                    Else
                        db_password = Password.Text
                        db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                            db_name & Class_Grade.Text & ".mdb" & Chr(34)
                        Set db_conn = New ADODB.Connection
                        On Error Resume Next
                        db_conn.Open db_drive
                        If db_conn.State = adStateClosed Then
                            MsgBox "密码有误！", vbOKOnly + vbCritical, "错误"
                            Exit Sub
                        Else
                            db_conn.Close
                            manage.Show
                            Unload Me
                        End If
                    End If
                End If
            Else
                db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                    db_name & table_name & ".mdb" & Chr(34)
                If Dir(db_name & Class_Grade.Text & ".mdb") = "" Then
                    Set catalog = New adox.catalog
                    Set db_table = New adox.Table
                    On Error GoTo DatabaseError
                    catalog.Create db_drive
                    catalog.ActiveConnection = db_drive
                    table_name = Class_Grade.Text
                    db_table.Name = table_name
                    db_table.Columns.Append "index", adox.DataTypeEnum.adInteger
                    db_table.Columns.Append "id", adox.DataTypeEnum.adInteger
                    db_table.Columns.Append "name", adox.DataTypeEnum.adWChar
                    db_table.Columns.Append "moral_score", adox.DataTypeEnum.adInteger
                    db_table.Columns.Append "explicit", adox.DataTypeEnum.adWChar, 200
                    catalog.Tables.Append db_table
                    manage.Show
                    Unload Me
                Else
                    db_password = Password.Text
                    db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                        db_name & Class_Grade.Text & ".mdb" & Chr(34)
                    Set db_conn = New ADODB.Connection
                    On Error Resume Next
                    db_conn.Open db_drive
                    If db_conn.State = adStateClosed Then
                        MsgBox "密码有误！", vbOKOnly + vbCritical, "错误"
                        Exit Sub
                    Else
                        db_conn.Close
                        manage.Show
                        Unload Me
                    End If
                End If
            End If
        Else
            db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                db_name & Class_Grade.Text & ".mdb" & Chr(34) & ";Jet OLEDB:Database" & _
                " Password=" & db_password
            'if no exist database and table
            'create them
            table_name = Class_Grade.Text
            If Dir(db_name & Class_Grade.Text & ".mdb") = "" Then
                Set catalog = New adox.catalog
                Set db_table = New adox.Table
                On Error GoTo DatabaseError
                catalog.Create db_drive
                catalog.ActiveConnection = db_drive
                table_name = Class_Grade.Text
                db_table.Name = table_name
                db_table.Columns.Append "index", adox.DataTypeEnum.adInteger
                db_table.Columns.Append "id", adox.DataTypeEnum.adInteger
                db_table.Columns.Append "name", adox.DataTypeEnum.adWChar
                db_table.Columns.Append "moral_score", adox.DataTypeEnum.adInteger
                db_table.Columns.Append "explicit", adox.DataTypeEnum.adWChar
                catalog.Tables.Append db_table
                manage.Show
                Unload Me
            Else
                db_password = Password.Text
                db_drive = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & _
                    db_name & Class_Grade.Text & ".mdb" & Chr(34) & ";Jet OLEDB:Database" & _
                    " Password=" & db_password
                Set db_conn = New ADODB.Connection
                On Error Resume Next
                db_conn.Open db_drive
                If db_conn.State = adStateClosed Then
                    MsgBox "密码有误！", vbOKOnly + vbCritical, "错误"
                    Exit Sub
                Else
                    db_conn.Close
                    manage.Show
                    Unload Me
                End If
            End If
        End If
    End If
    Exit Sub
DatabaseError:
    MsgBox "数据库操作失败，错误信息：" & Err.Description, vbOKOnly + vbExclamation, "提示"
    Exit Sub
End Sub

Private Sub Command2_Click()
    End
End Sub

Private Sub Form_Load()
    On Error GoTo Error
    db_name = App.Path
    If Right(db_name, 1) <> "\" Then 'if it is child directory
        If Dir(db_name & "\db", vbDirectory) = "" Then 'if \db directory is not exist
            'create a directory, name is \db
            MkDir db_name & "\db"
        End If
        db_name = db_name & "\db\"
    Else
        If Dir(db_name & "db", vbDirectory) = "" Then
            'same as
            MkDir db_name & "db"
        End If
        db_name = db_name & "db\"
    End If
    Dim file
    Set found_file = CreateObject("Scripting.FileSystemObject")
    Set folder = found_file.GetFolder(db_name)
    Set file_count = folder.Files
    Class_Grade.Clear
    For Each file In file_count
        Dim file_name
        file_name = Left(file.Name, InStr(1, file.Name, ".mdb") - 1)
        Class_Grade.AddItem file_name, Class_Grade.ListCount
    Next
    If Class_Grade.ListCount > 0 Then
        Class_Grade.ListIndex = 0
    End If
    Exit Sub
Error:
    MsgBox "出现错误，错误信息：" & Err.Description, vbOKOnly + vbCritical, "错误"
End Sub
